<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户信息</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layuimini/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/layuimini/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">

                            <div class="layui-input-inline">
                                <input type="text" style="width: 250px" name="searchContent" autocomplete="off" placeholder="请输入用户名、姓名或手机号" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>


    </div>
</div>
<script src="/layuimini/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/layuimini/js/MyUtils1.js" charset="utf-8"></script>
<script src="layuimini/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
</script>
<script type="text/html" id="barStart">
    <a id="open" class="layui-btn layui-btn-xs layui-btn-normal "  lay-event="open">连接</a>
    <a id="close" class="layui-btn layui-btn-xs layui-btn-warm"  lay-event="close">断开</a>

</script>
<script>
    var serverList=[];

    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        table.render({
            elem: '#currentTableId',
            url: '/getLinuxServer',
        headers:{
                "authorization":getCookie('authorization')
            },
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [
                {type: "checkbox", width: 50},
                {field: 'id', width: 60, title: 'ID', sort: true},
                {field: 'host', width: 160, title: '主机地址', align: "center"},
                {field: 'description', title: '描述', width: 120, align: "center"},
                {field: 'cpu', width: 120,title: 'CPU使用率', sort: true,align: "center"},
                {field: 'ram', width: 120, title: '内存使用率', sort: true,align: "center"},
                {field: 'disk', width: 120, title: '磁盘使用率', sort: true,align: "center"},
                    {field: 'status', title: '状态',toolbar: '#barStart', width: 100, align: "center"},
                    {title: '操作', minWidth: 120, toolbar: '#barDemo', align: "center"}
            ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
           // var resultData=postRequest("/admin/searchAdmin",result)
            //layer.alert(resultData)
           // console.log(resultData.data)
            //执行搜索重载
            table.reload('currentTableId', {
                url:"/admin/searchAdmin",
                method:"post",
                contentType: 'application/json',
                dataType : "json",
                 where:data.field
            });

            return false;
        });
        //监听状态是否开启
        form.on('switch(connStatus)', function(obj){
            if(obj.elem.checked){
                    layer.tips( "已开启", obj.othis,{time:800});
                    table.on('toolbar(currentTableFilter)', function(obj){
                        var data = obj.data;
                        if (obj.event === 'open') {  // 监听删除操作
                            var data=obj.data
                            serverList.push(data)
                            layer.alert(JSON.stringify(serverList));
                        }
                    });
            }else {
                layer.tips( "已关闭", obj.othis,{time:800});
            }

        });
        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                        var index = layer.open({
                            title: '添加用户',
                            type: 2,
                            shade: 0.2,
                            maxmin:true,
                            shadeClose: true,
                            area: ['50%', '100%'],
                            content: '/userAddPage'
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.confirm('真的删除么', function (index) {
                postRequest("/admin/deleteAdmin",JSON.stringify(data))
                window.location.reload();
                });
            }else if (obj.event === 'open') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    ,data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            console.log(data)
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['50%', '100%'],
                    content: '/userEditPage',
                    success(layero,index){
                        var iframe = window['layui-layer-iframe' + index];
                        iframe.child(data.id);
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                var data=obj.data
                layer.confirm('真的删除么', function (index) {
                    var arr=[];
                    arr.push(data)
                   console.log(JSON.stringify(arr))
                   // var d=(obj.data).serializeArray()
                    postRequest("/admin/deleteAdmin",JSON.stringify(arr))
                    layer.close(index);
                    window.location.reload();

                });
            }else if (obj.event === 'open') {  // 监听开启操作
                var data=obj.data
                serverList.push(data.id)
                console.log(serverList)
                var res=getRequest("/connServer",{serverId:data.id})
                $("#open").hide()
                $("#close").show()
                layer.alert(res.message);
            }else if (obj.event === 'close') {  // 监听关闭操作
                var data=obj.data
                layer.confirm('真的断开吗？', function (index) {
                $("#close").hide()
                $("#open").show()
                /*serverList.push(data.id)
                console.log(serverList)*/
                 var index = serverList.indexOf(data.id);
                 if (index > -1) {
                     serverList.splice(index, 1);
                 }
                 layer.alert(JSON.stringify(serverList));
                });
            }
        });
        setInterval(monitorServerInfo, 10000);
            function monitorServerInfo() {
                if(serverList.length>0) {
                    table.reload('currentTableId', {
                        url: "/execCmd",
                        method: "post",
                        contentType: 'application/json',
                        dataType: "json",
                        where: {"serverList": serverList}
                    });
                }

        }
    });


</script>

</body>
</html>